#include "AppRuntime.h"
#include <napi/env.h>

#include <napi/native_api.h>

#include <optional>

static napi_env persist_env = nullptr;

EXTERN_C_START
// 模块初始化
static napi_value Init(napi_env env, napi_value exports) {
    persist_env = env;
    return exports;
}
EXTERN_C_END
    
static napi_module nativeModule =
{
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "AppRuntime",
    .nm_priv = nullptr,
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterModule()
{
    napi_module_register(&nativeModule);
}

namespace Babylon
{
    void AppRuntime::RunEnvironmentTier(const char*)
    {
        Napi::Env env = Napi::Attach(persist_env);
        
        Run(env);
        
        Napi::Detach(env);
    }
}
